﻿using System.Collections.Generic;
using UnityEngine;
using EchoFramework;

/// <summary>
/// 地图生成器
/// </summary>
public class MapGenerator : ObjectBase
{
    /// <summary>
    /// 地图相关逻辑module
    /// </summary>
    private MapLogicModule module;

    /// <summary>
    /// 已经记录的Item
    /// </summary>
    private static MapRoadItem[,] mapRoadItemArr;

    /// <summary>
    /// x偏移 防止数组溢出
    /// </summary>
    private static int arrOffsetX;
    /// <summary>
    /// y偏移 防止数组溢出
    /// </summary>
    private static int arrOffsetY;

    public void Awake()
    {
        module = MapLogicModule.Instance;
    }

    /// <summary>
    /// 生成地图路线
    /// </summary>
    public void GenerateMap()
    {
        transform.ClearChildren();
        List<MapPosCfg> mapPosCfgList = module.LevelMapPosCfgList;
        int width = module.GetLevelWidth(out arrOffsetX);
        int height = module.GetLevelHeight(out arrOffsetY);
        mapRoadItemArr = new MapRoadItem[width + 1, height + 1];

        foreach (MapPosCfg mapPosCfg in mapPosCfgList)
        {
            List<MapPosCfg> targetCfgList = mapPosCfg.GetTargetIDCfgList();
            if (targetCfgList == null)
                continue;
            foreach (MapPosCfg singleTargetCfg in targetCfgList) //遍历每个目标点
            {
                BuildMapRoadItemFromA2B(mapPosCfg, singleTargetCfg);
            }
        }
    }

    /// <summary>
    /// 一条路的构建 从A到B
    /// </summary>
    /// <param name="startCfg">该条路起始点配置</param>
    /// <param name="endCfg">该条路终点配置</param>
    private void BuildMapRoadItemFromA2B(MapPosCfg startCfg, MapPosCfg endCfg)
    {
        bool isHorizontal = false;
        if (startCfg.PosCoordinate.X == endCfg.PosCoordinate.X &&
            startCfg.PosCoordinate.Y != endCfg.PosCoordinate.Y) //竖直方向
        {
            isHorizontal = false;
        }
        else if (startCfg.PosCoordinate.Y == endCfg.PosCoordinate.Y &&
                 startCfg.PosCoordinate.X != endCfg.PosCoordinate.X) //水平方向
        {
            isHorizontal = true;
        }
        else
        {
            GameLogger.LogError($"MapPos.xml ID:{startCfg.ID} 指向的目标坐标有错！");
            return;
        }

        MapRoadItem lastRoadItem = GetMapRoadItem(startCfg.PosCoordinate);
        if (lastRoadItem == null)
        {
            lastRoadItem = AssetManager.Instance.LoadGameObject<MapRoadItem>(
                "Assets/GameRes/GamePrefab/Map/MapRoadItem.prefab", transform);
            lastRoadItem.SetSelfData(startCfg);
            SetMapRoadItemArr(startCfg.PosCoordinate.X, startCfg.PosCoordinate.Y, lastRoadItem);
        }

        while (true)
        {
            MapRoadItem tempItem = null;
            Coordinate tempCoor = null;  
            if (isHorizontal)
            {
                int offset = startCfg.PosCoordinate.X - endCfg.PosCoordinate.X > 0 ? -1 : 1;
                tempCoor = new Coordinate(lastRoadItem.Coor.X + offset, lastRoadItem.Coor.Y);
            }
            else
            {
                int offset = startCfg.PosCoordinate.Y - endCfg.PosCoordinate.Y > 0 ? -1 : 1;
                tempCoor = new Coordinate(lastRoadItem.Coor.X, lastRoadItem.Coor.Y + offset);
            }

            tempItem = GetMapRoadItem(tempCoor);
            if (tempItem == null)
            {
                tempItem = AssetManager.Instance.LoadGameObject<MapRoadItem>(
                    "Assets/GameRes/GamePrefab/Map/MapRoadItem.prefab", transform);
                SetMapRoadItemArr(tempCoor.X, tempCoor.Y, tempItem);
            }
            if (endCfg.PosCoordinate.Equal(tempCoor))  //达到最终点位置
            {
                tempItem.SetSelfData(endCfg);
                tempItem.AddInMapPosCfg(startCfg);
                return;
            }
            else  //普通道路
            {
                tempItem.SetNullData(tempCoor, isHorizontal);
                tempItem.AddInMapPosCfg(startCfg);
                tempItem.AddOutMapPosCfg(endCfg);
            }
            lastRoadItem = tempItem;
        }
    }

    /// <summary>
    /// 返回已记录的MapRoadItem
    /// </summary>
    /// <param name="coor">坐标</param>
    public static MapRoadItem GetMapRoadItem(Coordinate coor)
    {
        if (coor == null)
            return null;
        return GetMapRoadItem(coor.X, coor.Y);
    }

    /// <summary>
    /// 返回已记录的MapRoadItem
    /// </summary>
    /// <param name="x">坐标X</param>
    /// <param name="y">坐标Y</param>
    public static MapRoadItem GetMapRoadItem(int x, int y)
    {
        int correctX = x + arrOffsetX;
        int correctY = y + arrOffsetY;
        MapRoadItem mapRoadItem = mapRoadItemArr[correctX, correctY];
        return mapRoadItem;
    }

    /// <summary>
    /// 存储数组
    /// </summary>
    /// <param name="x">x坐标</param>
    /// <param name="y">y坐标</param>
    /// <param name="item">目标MapRoadItem对象</param>
    private void SetMapRoadItemArr(int x, int y, MapRoadItem item)
    {
        int correctX = x + arrOffsetX;
        int correctY = y + arrOffsetY;
        mapRoadItemArr[correctX, correctY] = item;
    }
}